package com.hhj.demo.ImportDemo;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * Created by KING on 2018/7/18.
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     * AnnotationMetadata：当前类的注解信息
     * BeanDefinitionRegistry:BeanDefinition注册类；
     * 把所有需要添加到容器中的bean；调用
     * BeanDefinitionRegistry.registerBeanDefinition手工注册进来
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
        boolean definition = registry.containsBeanDefinition(Yellow.class.getName());
        boolean definition2 = registry.containsBeanDefinition(Blue.class.getName());
        if (definition && definition2) {
            // 指定Bean定义信息；（Bean的类型，Bean。。。）
            RootBeanDefinition beanDefinition = new RootBeanDefinition(Red.class);
            // 注册一个Bean，指定bean名
            registry.registerBeanDefinition(Red.class.getName(), beanDefinition);
        }


        //手动注入实例
        BeanDefinitionBuilder beanDef_forwards = BeanDefinitionBuilder.rootBeanDefinition(Yellow.class);
        registry.registerBeanDefinition("forwards", beanDef_forwards.getBeanDefinition());

        BeanDefinitionBuilder beanDef_security = BeanDefinitionBuilder.rootBeanDefinition(Blue.class);
        registry.registerBeanDefinition("security", beanDef_security.getBeanDefinition());

    }
}

